package com.peteralbus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.peteralbus.entity.EarthquakeInfo;
import com.peteralbus.entity.IntensityLine;
import com.peteralbus.mapper.EarthquakeInfoMapper;
import com.peteralbus.mapper.IntensityLineMapper;
import com.peteralbus.service.EarthquakeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


/**
 * The type Earthquake info service.
 * @author PeterAlbus
 * Created on 2022/1/20.
 */
@Service
//首先，在applicationContext.xml文件中加一行：
//<context:component-scan base-package="com.hzhi.clas"/>
//加上这一行以后，将自动扫描路径下面的包，如果一个类带了@Service注解，将自动注册到Spring容器，不需要再在applicationContext.xml文件定义bean了，
// 类似的还包括@Component、@Repository、@Controller
// 1、 其getBean的默认名称是类名（头字母小写），可以@Service(“xxxx”)这样来指定，
// 2、其定义的bean默认是单例的，可以使用@Service(“beanName”) @Scope(“prototype”)来改变。
// 3、可以通过@PostConstruct和@PreDestroy指定初始化方法和销毁方法（方法名任意）
public class EarthquakeInfoServiceImpl implements EarthquakeInfoService
{
    /**
     * The Earthquake info mapper.
     */
    EarthquakeInfoMapper earthquakeInfoMapper;
    /**
     * The Intensity line mapper.
     */
    IntensityLineMapper intensityLineMapper;

    /**
     * Sets earthquake info mapper.
     *
     * @param earthquakeInfoMapper the earthquake info mapper
     */
    @Autowired //当我们在将一个类上标注@Service或者@Controller或@Component或@Repository注解之后，
    // spring的组件扫描就会自动发现它，并且会将其初始化为spring应用上下文中的bean。
    // 当需要使用这个bean的时候，例如加上@Autowired注解的时候，这个bean就会被创建。
    public void setEarthquakeInfoMapper(EarthquakeInfoMapper earthquakeInfoMapper)
    {
        this.earthquakeInfoMapper = earthquakeInfoMapper;
    }

    /**
     * Sets intensity line mapper.
     *
     * @param intensityLineMapper the intensity line mapper
     */
    //1. 可以给你当作注释用，感觉这个也不能说明什么，注释也没什么用。
    //2. 可以告诉读你代码的人，这是对它父类方法的重写，其实很多代码规范没有为什么，规范就是规范，代码的可读性还是很重要的。
    //3. 编译器可以给你验证@Override下面的方法名称是否是你父类中所有的，如果没有就会报错。
    @Autowired
    public void setIntensityLineMapper(IntensityLineMapper intensityLineMapper)
    {
        this.intensityLineMapper = intensityLineMapper;
    }
    @Override
    public boolean judgeEarthquakeExist(String name, LocalDateTime time){
        return earthquakeInfoMapper.judgeEarthquakeExist(name,time);
    }
    @Override
    public List<EarthquakeInfo> queryInfoWithLine(Map<String, Object> parameter)
    {
        return earthquakeInfoMapper.queryInfoWithLine(parameter);
    }
    @Override
    public List<EarthquakeInfo> getHistoryList(String name){
        return earthquakeInfoMapper.getHistoryList(name);
    }
    @Override
    public int insertEarthquakeInfo(EarthquakeInfo earthquakeInfo)
    {
        int result = 0;
        result = earthquakeInfoMapper.insert(earthquakeInfo); //mybatis
        if (result == 0)
        {
            return result;
        }
        for (IntensityLine inensityLine : earthquakeInfo.getIntensityLineList())
        {
            inensityLine.setEarthquakeId(earthquakeInfo.getEarthquakeId());
            result = intensityLineMapper.insert(inensityLine);
        }
        return result;
    }
    @Override
    public int deleteEarthquakeInfo(EarthquakeInfo earthquakeInfo)
    {
        int result = 0;
        QueryWrapper<IntensityLine> intensityLineQueryWrapper = new QueryWrapper<>();
        intensityLineQueryWrapper.eq("earthquake_id", earthquakeInfo.getEarthquakeId());
        result = intensityLineMapper.delete(intensityLineQueryWrapper);
        if (result > 0)
        {
            result = earthquakeInfoMapper.deleteById(earthquakeInfo);
        }
        return result;
    }
    @Override
   public EarthquakeInfo getEarthquakeInfoById(long id){
        return earthquakeInfoMapper.selectById(id);
    }
    @Override
  public   List<EarthquakeInfo> getSeismicZonesData(Double left,Double right,Double top,Double bottom){
        return earthquakeInfoMapper.getSeismicZonesData(left,right,top,bottom);
  }
}
